package com.kang.controller;

import com.kang.aop.operLog.OperLog;
import com.kang.entity.SysRole;
import com.kang.menu.BusinessType;
import com.kang.service.SysRoleService;
import com.kang.util.WebResponse;
import com.kang.vo.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
@Slf4j
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 查询所有的角色
     */
    @ApiOperation("查询所有的角色")
    @GetMapping("findAll")
    public WebResponse findAll() {
        try {
            return WebResponse.resSuccess("查询所有的角色成功",sysRoleService.findAll());
        } catch (Exception ex) {
            log.info("查询所有的角色失败",ex);
            return WebResponse.resFail("查询所有的角色失败",null);
        }

    }

    /**
     * 逻辑删除角色
     */
    @OperLog(title = "逻辑删除角色", businessType = BusinessType.DELETE)
    @ApiOperation("逻辑删除角色")
    @DeleteMapping("remove/{id}")
    public WebResponse removeRole(@PathVariable Long id) {
        try {
            return WebResponse.resSuccess("逻辑删除角色成功",sysRoleService.removeRole(id));
        } catch (Exception ex) {
            log.info("逻辑删除角色失败",ex);
            return WebResponse.resFail("逻辑删除角色失败",null);
        }
    }

    /**
     * 条件分页查询角色
     */
    @ApiOperation("条件分页查询角色")
    @GetMapping("{page}/{limit}")
    public WebResponse findQueryRole(@PathVariable("page") Long page, @PathVariable("limit") Long limit, SysRoleQueryVo sysRoleQueryVo) {
        try {
            return WebResponse.resSuccess("条件分页查询角色成功",sysRoleService.findQueryRole(page,limit,sysRoleQueryVo));
        } catch (Exception ex) {
            log.info("条件分页查询角色失败",ex);
            return WebResponse.resFail("条件分页查询角色失败",null);
        }
    }

    /**
     * 添加角色
     */
    @OperLog(title = "角色管理", businessType = BusinessType.INSERT)
    @ApiOperation("添加角色")
    @PostMapping("saveRole")
    public WebResponse saveRole(@RequestBody SysRole sysRole) {
        try {
            return WebResponse.resSuccess("添加角色成功",sysRoleService.saveRole(sysRole));
        } catch (Exception ex) {
            log.info("添加角色失败",ex);
            return WebResponse.resFail("添加角色失败",null);
        }
    }

    /**
     * 修改角色
     */
    @OperLog(title = "修改角色", businessType = BusinessType.UPDATE)
    @ApiOperation("修改角色")
    @PostMapping("updateRole")
    public WebResponse updateRole(@RequestBody SysRole sysRole) {
        try {
            return WebResponse.resSuccess("修改角色成功",sysRoleService.updateRole(sysRole));
        } catch (Exception ex) {
            log.info("修改角色失败",ex);
            return WebResponse.resFail("修改角色失败",null);
        }
    }

    /**
     * 批量删除
     */
    @OperLog(title = "批量删除角色", businessType = BusinessType.DELETE)
    @ApiOperation("批量删除")
    @PostMapping("batchDeleteRole")
    public WebResponse batchDeleteRole(@RequestBody List<String> ids) {
        try {
            return WebResponse.resSuccess("批量删除成功",sysRoleService.batchDeleteRole(ids));
        } catch (Exception ex) {
            log.info("批量删除失败",ex);
            return WebResponse.resFail("批量删除失败",null);
        }
    }

    /**
     * 根据ID查询接口
     */
    @ApiOperation("根据ID查询接口")
    @GetMapping("getRoleById/{id}")
    public WebResponse getRoleById(@PathVariable Long id) {
        try {
            return WebResponse.resSuccess("根据ID查询接口成功",sysRoleService.getRoleById(id));
        } catch (Exception ex) {
            log.info("根据ID查询接口失败",ex);
            return WebResponse.resFail("根据ID查询接口失败",null);
        }
    }


}
